From dbd877f87f351970a6b5f659ab88531f76c4b7d8 Mon Sep 17 00:00:00 2001 From: Robert Lipe Date: Sun, 29 Jul 2018 20:48:54 -0500 Subject: [PATCH] More data structure work in xcsv Push QStringification up two levels. Keep table and callers of substations in Qt-land to keep QHash happy and reduce copy/leak in most cases. --- csv_util.cc | 6 +++--- csv_util.h | 6 +++--- xcsv.cc | 27 +++++++++++++-------------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/csv_util.cc b/csv_util.cc index 24b31290e..457480d0a 100644 --- a/csv_util.cc +++ b/csv_util.cc @@ -1676,9 +1676,9 @@ xcsv_waypt_pr(const Waypoint* wpt) buff = QString().sprintf(fmp->printfc, waypt_out_count + atoi(fmp->val)); break; case XT_CONSTANT: { - const char* cp = xcsv_get_char_from_constant_table(fmp->val); - if (cp) { - buff = QString().sprintf(fmp->printfc, cp); + auto cp = xcsv_get_char_from_constant_table(fmp->val); + if (!cp.isEmpty()) { + buff = QString().sprintf(fmp->printfc, CSTR(cp)); } else { buff = QString().sprintf(fmp->printfc, fmp->val); } diff --git a/csv_util.h b/csv_util.h index 47de05d2d..68884b53c 100644 --- a/csv_util.h +++ b/csv_util.h @@ -77,7 +77,7 @@ xcsv_ofield_add(const char*, const char*, const char*, int options); void xcsv_destroy_style(void); -const char* +QString xcsv_get_char_from_constant_table(const char* key); /****************************************************************************/ @@ -99,8 +99,8 @@ typedef struct field_map { /* something to map config file constants to chars */ typedef struct char_map { - const char* key; - const char* chars; + const QString key; + const QString chars; } char_map_t; namespace gpsbabel diff --git a/xcsv.cc b/xcsv.cc index 75c1b44d2..dde32432c 100644 --- a/xcsv.cc +++ b/xcsv.cc @@ -190,30 +190,29 @@ xcsv_destroy_style(void) } // Given a keyword of "COMMASPACE", return ", ". -const char* +QString xcsv_get_char_from_constant_table(const char* key) { static QHash substitutions; if (substitutions.empty()) { - for (char_map_t* cm = xcsv_char_table; cm->key; cm++) { + for (char_map_t* cm = xcsv_char_table; !cm->key.isNull(); cm++) { substitutions.insert(cm->key, cm->chars); } } - if (!substitutions.contains(key)) { - return xstrdup(key); + if (substitutions.contains(key)) { + return substitutions[key]; } - // QHash is very unhappy with raw character pointers. - // This is almost certainly a leak and IS certainly in bad taste, but - // all the callers seem to free it quickly. - return xstrdup(substitutions[key]); + // This seems like it should be bad, but we have formats depending on it!?! + // Warning() << "Unknown substitution keyword" << key; + return QString(); } static void xcsv_parse_style_line(char* sbuff) { char* sp; - char* p; - const char* cp; + char *p; + QString cp; const char* key, *val, *pfc; /* @@ -236,7 +235,7 @@ xcsv_parse_style_line(char* sbuff) if (ISSTOKEN(sbuff, "FIELD_DELIMITER")) { sp = csv_stringtrim(&sbuff[16], "\"", 1); cp = xcsv_get_char_from_constant_table(sp); - if (cp) { + if (!cp.isEmpty()) { xcsv_file.field_delimiter = cp; xfree(sp); } else { @@ -258,7 +257,7 @@ xcsv_parse_style_line(char* sbuff) if (ISSTOKEN(sbuff, "FIELD_ENCLOSER")) { sp = csv_stringtrim(&sbuff[15], "\"", 1); cp = xcsv_get_char_from_constant_table(sp); - if (cp) { + if (!cp.isEmpty()) { xcsv_file.field_encloser = cp; xfree(sp); } else { @@ -273,7 +272,7 @@ xcsv_parse_style_line(char* sbuff) if (ISSTOKEN(sbuff, "RECORD_DELIMITER")) { sp = csv_stringtrim(&sbuff[17], "\"", 1); cp = xcsv_get_char_from_constant_table(sp); - if (cp) { + if (!cp.isEmpty()) { xcsv_file.record_delimiter = cp; xfree(sp); } else { @@ -325,7 +324,7 @@ xcsv_parse_style_line(char* sbuff) sp = csv_stringtrim(&sbuff[9], "\"", 1); cp = xcsv_get_char_from_constant_table(sp); - if (cp) { + if (!cp.isEmpty()) { p = xstrdup(cp); xfree(sp); } else { -- 2.30.2